package com.example.chenzhiran.myapplication7.server;
;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.sql.ResultSet;
import java.sql.Statement;

public class loginPage {
    private Socket socket;
    private clientData indata;
    private serverData outdata = new serverData();
    Statement statement;
    ObjectOutputStream out;

    public loginPage(Socket mysocket, clientData mydata, Statement stmt)
    {
        this.socket = mysocket;
        this.indata = mydata;
        this.statement = stmt;

        switch(indata.dataId)
        {
            case 0:
                System.out.println("客户登录......");
                login();
                break;
            case 1:
                System.out.println("客户注册......");
                regist();
                break;
            default:
                System.out.println("找回密码......");
                forgetpassword();
                break;
        }

        try {
            out = new ObjectOutputStream(socket.getOutputStream());
            out.writeObject(outdata);
            out.close();
            System.out.printf("登录页数据发送完毕\n");
        }catch(Exception e)
        {
            e.printStackTrace();
        }
    }

    /*描述：登录功能
     *参数：空
     *返回：空
     */
    public void login()
    {
        //查看username以及password字段是否为空
        if(indata.username == null || indata.username.length() == 0)
        {
            outdata.datastatus = 1;
            return;
        }
        if(indata.userpassword == null || indata.userpassword.length() == 0)
        {
            outdata.datastatus = 1;
            return;
        }

        //检索密码和用户名是否相同
        boolean finduser = false;
        try
        {
            String sql = "SELECT * FROM user";
            ResultSet rs = statement.executeQuery(sql);
            //通过字段检索
            while(rs.next())
            {
                String name = rs.getString("username");
                String password = rs.getString("password");
                if(name.equals(indata.username) && password.equals(indata.userpassword))
                {
                    finduser = true;
                    break;
                }
            }
            rs.close();
        }catch(Exception e)
        {
            e.printStackTrace();
        }

        //根据检索结果返回值
        if(finduser == true)
        {
            outdata.datastatus = 0;
        }
        else
        {
            outdata.datastatus = 1;
            outdata.message = "请输入正确的用户名和密码";
        }

    }

    /*描述：注册功能
     *参数：空
     *返回：空
     */
    public void regist()
    {
        //查看用户是否已经注册
        try
        {
            String sql = "SELECT * FROM user";
            ResultSet rs = statement.executeQuery(sql);
            while(rs.next())
            {
                String name = rs.getString("username");
                if(name.equals(indata.username))
                {
                    outdata.datastatus = 1;
                    outdata.message = "用户已经存在";
                    rs.close();
                    return;
                }
            }
            rs.close();
        }catch(Exception e)
        {
            e.printStackTrace();
        }

        //插入用户名和密码
        try
        {
            String sql = "insert into user (username, password) value (" + "\"" + indata.username + "\"" + "," + "\"" + indata.userpassword + "\"" + ")";
            int rs = statement.executeUpdate(sql);
        }catch(Exception e)
        {
            outdata.datastatus = 1;
            e.printStackTrace();
        }
        outdata.datastatus = 0;
    }

    /*描述：找回密码功能
     *参数：空
     *返回：空
     */
    public void forgetpassword()
    {
        boolean finduser = false;
        try
        {
            String sql = "SELECT * FROM user";
            ResultSet rs = statement.executeQuery(sql);
            while(rs.next())
            {
                String name = rs.getString("username");
                String password = rs.getString("password");
                if(name.equals(indata.username))
                {
                    finduser = true;
                    outdata.datastatus = 0;
                    outdata.userpassword = password;
                    break;
                }
            }
            rs.close();
        }catch(Exception e)
        {
            e.printStackTrace();
        }

        if(finduser == false)
        {
            outdata.datastatus = 1;
            outdata.message = "没有此用户";
        }
    }
}
